#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : xuexiaohua
# @time    : 2025/8/23 16:43
# @function: 这个文件作用：共享数据/方法
# @version :
import os
import pytest

from netPan123.utils.file_util import FileUtil
from netPan123.utils.time_util import TimeUtil

# 设置当前文件的所在目录(固定路径相对起始位置)
PROJECT_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
print(PROJECT_ROOT_DIR)
# 设置执行数据存储yaml文件
todayStr = TimeUtil.localtime_to_str("%Y%m%d")
USER_File_PATH = os.path.join(PROJECT_ROOT_DIR, 'temp', todayStr)
FileUtil.create_folder(USER_File_PATH)
print(USER_File_PATH)

@pytest.fixture(scope="session")
def login_2():
    print("===========login_2 start===========")
    yield
    print("===========login_2 end===========")

# 给print_args_and_result装饰器添加注释
def print_args_and_result(func):
    """
    装饰器：在调用被装饰的测试函数前后，打印其参数和返回值，便于调试和日志追踪。
    """
    def wrapper(*args, **kwargs):
        logger.info(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
        result = func(*args, **kwargs)
        logger.info(f"{func.__name__} returned: {result}")
        return result
    return wrapper

# 给pytest_collection_modifyitems钩子函数添加注释
def pytest_collection_modifyitems(items):
    """
    pytest钩子函数：在收集到的测试用例函数上自动应用print_args_and_result装饰器，
    使所有以'test_'开头的测试函数在执行时自动打印参数和返回值，避免重复装饰。
    """
    for item in items:
        # item.obj is the test function object
        if hasattr(item, "obj") and callable(item.obj):
            if item.name.startswith("test_"):
                # Avoid double-wrapping
                if getattr(item.obj, "_is_wrapped_with_print_args_and_result", False):
                    continue
                wrapped = print_args_and_result(item.obj)
                wrapped._is_wrapped_with_print_args_and_result = True
                item.obj = wrapped


